#include "Cube.h"

//Static members
const unsigned int Cube::vertexCount = 8;
const unsigned int Cube::faceCount = 6;

Cube::Cube(Vector3* position, Vector3* scale, Vector3* color) : DrawablePrimitive(position, scale)
{
	this->verticies = new Vector3*[8];
	//Set up 8 verticies required to draw a cube.
	this->verticies[0] = new Vector3(1.0f, 1.0f, 1.0f);
	this->verticies[1] = new Vector3(-1.0f, 1.0f, 1.0f);
	this->verticies[2] = new Vector3(-1.0f, 1.0f, -1.0f);
	this->verticies[3] = new Vector3(1.0f, 1.0f, -1.0f);
	this->verticies[4] = new Vector3(1.0f, -1.0f, 1.0f);
	this->verticies[5] = new Vector3(-1.0f, -1.0f, 1.0f);
	this->verticies[6] = new Vector3(-1.0f, -1.0f, -1.0f);
	this->verticies[7] = new Vector3(1.0f, -1.0f, -1.0f);

	this->faceColors = new Vector3*[6];
	//Assign colors to faces
	for(int i = 0; i < this->faceCount; i++)
		this->faceColors[i] = color;

}


Cube::~Cube(void)
{
	for(int i = 0; i <= 7; i++)
	{
		delete this->verticies[i];
	}
}

void Cube::Draw()
{
		glBegin(GL_QUADS);

			//Set the color
			glColor3f(this->faceColors[TOP]->x, this->faceColors[TOP]->y, this->faceColors[TOP]->z);
			//Define the verticies
			glVertex3f(this->verticies[0]->x, this->verticies[0]->y, this->verticies[0]->z);
			glVertex3f(this->verticies[1]->x, this->verticies[1]->y, this->verticies[1]->z);
			glVertex3f(this->verticies[2]->x, this->verticies[2]->y, this->verticies[2]->z);
			glVertex3f(this->verticies[3]->x, this->verticies[3]->y, this->verticies[3]->z);

			//Bottom
			glColor3f(this->faceColors[BOTTOM]->x, this->faceColors[BOTTOM]->y, this->faceColors[BOTTOM]->z);
			glVertex3f(this->verticies[4]->x, this->verticies[4]->y, this->verticies[4]->z);
			glVertex3f(this->verticies[5]->x, this->verticies[5]->y, this->verticies[5]->z);
			glVertex3f(this->verticies[6]->x, this->verticies[6]->y, this->verticies[6]->z);
			glVertex3f(this->verticies[7]->x, this->verticies[7]->y, this->verticies[7]->z);

			//Front
			glColor3f(this->faceColors[FRONT]->x, this->faceColors[FRONT]->y, this->faceColors[FRONT]->z);
			glVertex3f(this->verticies[3]->x, this->verticies[3]->y, this->verticies[3]->z);
			glVertex3f(this->verticies[2]->x, this->verticies[2]->y, this->verticies[2]->z);
			glVertex3f(this->verticies[6]->x, this->verticies[6]->y, this->verticies[6]->z);
			glVertex3f(this->verticies[7]->x, this->verticies[7]->y, this->verticies[7]->z);

			//Back
			glColor3f(this->faceColors[BACK]->x, this->faceColors[BACK]->y, this->faceColors[BACK]->z);
			glVertex3f(this->verticies[0]->x, this->verticies[0]->y, this->verticies[0]->z);
			glVertex3f(this->verticies[1]->x, this->verticies[1]->y, this->verticies[1]->z);
			glVertex3f(this->verticies[5]->x, this->verticies[5]->y, this->verticies[5]->z);
			glVertex3f(this->verticies[4]->x, this->verticies[4]->y, this->verticies[4]->z);

			//Left
			glColor3f(this->faceColors[LEFT]->x, this->faceColors[LEFT]->y, this->faceColors[LEFT]->z);
			glVertex3f(this->verticies[2]->x, this->verticies[2]->y, this->verticies[2]->z);
			glVertex3f(this->verticies[1]->x, this->verticies[1]->y, this->verticies[1]->z);
			glVertex3f(this->verticies[5]->x, this->verticies[5]->y, this->verticies[5]->z);
			glVertex3f(this->verticies[6]->x, this->verticies[6]->y, this->verticies[6]->z);

			//Right
			glColor3f(this->faceColors[RIGHT]->x, this->faceColors[RIGHT]->y, this->faceColors[RIGHT]->z);
			glVertex3f(this->verticies[3]->x, this->verticies[3]->y, this->verticies[3]->z);
			glVertex3f(this->verticies[0]->x, this->verticies[0]->y, this->verticies[0]->z);
			glVertex3f(this->verticies[4]->x, this->verticies[4]->y, this->verticies[4]->z);
			glVertex3f(this->verticies[7]->x, this->verticies[7]->y, this->verticies[7]->z);

		glEnd();
}

void Cube::SetFaceColor(CubeFace face, Vector3* color)
{
	//delete this->faceColors[face]; //Hmmm...
	this->faceColors[face] = color;
}
